package Aleehot100;

/**
 * ClassName: c089
 * Package: Aleehot100
 * Description:416. 分割等和子集
 *
 * @Author BCXJ
 * @Create 2025/9/13 18:15
 * @Version 1.0
 * @Since 1.0
 */
public class c089 {
    public static void main(String[] args) {

    }

    public static boolean canPartition(int[] nums) {
        if(null == nums || nums.length == 0) return false;
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        // 奇数和无法平分
        if(sum % 2 != 0) return false;

        int target = sum / 2;
        boolean[] dp = new boolean[target + 1];
        dp[0] = true;

        for (int num : nums) {
            // 遍历元素
            for(int j = target; j >= num; j --) {
                if(dp[j -  num]) {
                    dp[j] = true;
                }
            }
            if(dp[target]) {
                return true;
            }
        }
        return dp[target];
    }
}
